/*
 * ----------------------------------------------------------------------------
 * Copyright (c) 2013-2014, xSky <guozhw at gmail dot com>
 * All rights reserved.
 * Distributed under GPL license.
 * ----------------------------------------------------------------------------
 */

#include "xRedisClient.h"
#include <sstream>


bool xRedisClient::zadd(const RedisDBIdx& dbi, const KEY& key,   const VALUES& vValues, int64_t& count){
    VDATA vCmdData;
    vCmdData.push_back("ZADD");
    vCmdData.push_back(key);
    addparam(vCmdData, vValues);
    return commandargv_integer(dbi, vCmdData, count);
}

bool xRedisClient::zscrad(const RedisDBIdx& dbi,     const string& key, int64_t& count){
    return command_integer(dbi, count, "ZCARD %s", key.c_str());
}

bool xRedisClient::zcount(const RedisDBIdx & dbi, const string & key, int min_score, int max_score, int64_t & count)
{
	return command_integer(dbi, count, "ZCOUNT %s %d %d", key.c_str(), min_score, max_score);
}

bool xRedisClient::zincrby(const RedisDBIdx& dbi,  const string& key, const double &increment, const string& member, string& value ) {
    return command_string(dbi, value, "ZINCRBY %s %f %s", key.c_str(), increment, member.c_str());
}

bool xRedisClient::zrange(const RedisDBIdx& dbi,  const string& key, int start, int end, VALUES& vValues, bool withscore) {
    if (withscore) {
        return command_list(dbi, vValues, "ZRANGE %s %d %d %s", key.c_str(), start, end, "WITHSCORES");
    }
    return command_list(dbi, vValues, "ZRANGE %s %d %d", key.c_str(), start, end);
}

bool xRedisClient::zrank(const RedisDBIdx& dbi,  const string& key, const string& member, int64_t &rank) {
    return command_integer(dbi, rank, "ZRANK %s %s", key.c_str(), member.c_str());
}

bool xRedisClient::zrem(const RedisDBIdx& dbi,        const KEY& key, const VALUES& vmembers, int64_t &count) {
    VDATA vCmdData;
    vCmdData.push_back("ZREM");
    vCmdData.push_back(key);
    addparam(vCmdData, vmembers);
    return commandargv_integer(dbi, vCmdData, count);
}

bool xRedisClient::zremrangebyrank(const RedisDBIdx& dbi,  const string& key, const int start, const int stop, int64_t& count) {
    return command_integer(dbi, count, "ZREMRANGEBYRANK %s %d %d", key.c_str(), start, stop);
}

bool xRedisClient::zrevrange(const RedisDBIdx& dbi,  const string& key, int start, int end, VALUES& vValues, bool withscore) {
    if (withscore) {
        return command_list(dbi, vValues, "ZREVRANGE %s %d %d %s", key.c_str(), start, end, "WITHSCORES");
    }
    return command_list(dbi, vValues, "ZREVRANGE %s %d %d", key.c_str(), start, end);
}

 bool xRedisClient::zrevrank(const RedisDBIdx& dbi,  const string& key, const string &member, int64_t& rank){
     return command_integer(dbi, rank, "ZREVRANK %s %s", key.c_str(), member.c_str());
 }

 bool xRedisClient::zscore(const RedisDBIdx& dbi,  const string& key, const string &member, string& score ){
     return command_string(dbi, score, "ZSCORE %s %s", key.c_str(), member.c_str());
 }


